var express = require("express");
var router = express.Router();
var db = require('../db/mysql');
var marked = require('marked');


//添加文章
router.post('/addArticle', function(req, res){
	let conten = req.body;
	let nowdata = Date.parse(new Date());

	conten.conten = marked(conten.conten)
	console.log(conten.conten)
	let that = res
	db.query(`INSERT INTO Article (wId, uId, title,content,pushTime,imgUrl,classid) VALUES ('', '${conten.uId}', '${conten.wtitle}', '${conten.conten}', '${nowdata}','${conten.imgurl}','${conten.mjid}')`, function(err,reuse){
		if(err){
			console.log(err);
		}else{
			let data = {
				code: 0,
				data: [],
				msg: "发布成功"
			}
			that.json(data);
		}
	});
})
//查询分类菜单
router.get("/getClassMenu", function(req, res){
	console.log(req.url);
	let sql = "SELECT * FROM classification"
	db.query(sql, function(err, reuse){
		if(err){console.log(err)}
		else{
			let data = {
				code: 0,
				msg: "获取成功",
				data:reuse
			}
			res.json(data)
		}
	});
})
//查询某个分类的文章
router.get("/getAllArticle", function(req, res){
	console.log(req.url)
	let classId = req.query.classId;

	let pageNo = req.query.pageNo?req.query.pageNo:1;
	let pagesize = req.query.pagesize?req.query.pagesize:3;
	
	let allsql = ""
	if(classId==""){
		allsql = `
		select a.wId , count(b.id) count, a.title, a.content, a.lookUp, a.pushTime, a.uId, a.imgUrl, u.id, u.userName, a.classId, c.classname 
		from article a 
		LEFT JOIN classification c on a.classId = c.classmj 
		LEFT JOIN user u on a.uId = u.id
		LEFT JOIN article_comment b on a.wId = b.Wid
		group by a.wId
		order by a.pushTime desc
		LIMIT ${(pageNo-1)*pagesize}, ${pagesize}
		`
	}else{
		allsql = `
		select a.wId , count(b.id) count, a.title, a.content, a.lookUp, a.pushTime, a.uId, a.imgUrl, u.id, u.userName, a.classId, c.classname 
		from article a 
		LEFT JOIN classification c on a.classId = c.classmj 
		LEFT JOIN user u on a.uId = u.id 
		LEFT JOIN article_comment b on a.wId = b.Wid WHERE a.classId=${classId} 
		group by a.wId 
		order by a.pushTime desc
		LIMIT ${(pageNo-1)*pagesize}, ${pagesize}
		`
	}
	db.query(allsql, function(err, reuse){
		if(err){console.log(err)}
		else{
			let data = {
				code: 0,
				msg: "获取成功",
				data: reuse
			}
			res.json(data);
		}
	});
});
//查询文章详情
router.get('/getArticleDetails', function(req, res){
	let wId = req.query.wId;
	console.log(req.url);
	let countNum = ""
	let counsql = `select count(*) as count from article_comment WHERE Wid=${wId}`
	db.query(counsql,function(err,rescoun){
		countNum = rescoun[0].count
	})
	let that = res;
	db.query("select a.wId wId, a.title title, a.content content, a.lookUp lookUp, a.pushTime pushTime, a.uId uId, a.imgUrl imgUrl, u.userName userName from article a left join user u on a.uId = u.id WHERE a.wId="+wId, function(err, reuse){
		if(err){console.log(err)}
		else{
			let gla = reuse[0].lookUp;
			gla++
			db.query("update article set lookUp="+gla+" where wId="+wId,function(err, reuse){
				if(err){
					console.log("浏览次数有问题了");
				}
			})
			reuse[0].lookUp=gla
			let data={
				code:0,
				data:reuse[0],
				msg:"获取成功"
			}
			
			
			
			let sql = `select
			a.wId wId, c.Uid uid, u.userName userneme, c.conten comm_conten, c.comm_time commtime, c.username username2 
			from (
				article_comment c LEFT JOIN article a on a.wId = c.Wid LEFT JOIN user u on c.Uid = u.id
			)
			WHERE a.wId = `+wId+` order by c.comm_time desc`
			db.query(sql, function(err, resdata){
				if(err){console.log(err);}
				else{
					data.data.commdata=resdata;
					data.data.comm_countnum = countNum
					that.json(data);
				}
			})
			
		}
	})
})

//评论文章
router.post("/userComment", function(req, res){
	console.log(req.body);
	let postconten = req.body;
	let comm_time = Date.parse(new Date());
	let sql = "INSERT INTO article_comment (id, username, Email, Website, conten, comm_time, Uid, Wid) VALUES ('', '"+postconten.username+"', '"+postconten.Email+"', '"+postconten.website+"', '"+postconten.conten+"', '"+comm_time+"', '"+postconten.Uid+"', '"+postconten.Wid+"')"
	db.query(sql, function(err, reuse){
		if(err){
			console.log(err)
		}else{
			let data = {
				code:0,
				msg:"评论成功",
				data:[]
			}
			res.json(data);
		}
	})
})



module.exports = router;